package cn.xxxy.fourm.Controller;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import cn.xxxy.fourm.dao.BoardDao;
import cn.xxxy.fourm.dao.PostDao;
import cn.xxxy.fourm.dao.UserDao;
import cn.xxxy.fourm.entity.Board;
import cn.xxxy.fourm.entity.Post;
import cn.xxxy.fourm.entity.User;
import cn.xxxy.fourm.model.BoardVo;
import cn.xxxy.fourm.model.DataGrid;
import cn.xxxy.fourm.model.Message;
import cn.xxxy.fourm.model.PostDto;
import cn.xxxy.fourm.model.UserVo;
import cn.xxxy.fourm.service.BoardService;
import cn.xxxy.fourm.service.BoardStateService;
import cn.xxxy.fourm.service.PostService;
@Controller
public class PostController {
	@Autowired
	private PostService postService;
	@Autowired
	private PostDao postDao;
	@Autowired
	private BoardDao boardDao;
	@Autowired
	private UserDao userDao;
	/**
	 * 1.显示
	 * @return
	 */
	@RequestMapping("/listPosts")
	public String listPosts() {
		return "/post/posts";
	}
	/**
     * 2、显示列表数据
     * @param page
     * @param rows
     * @return
     */ 
	@RequestMapping("/listPost")
	@ResponseBody
	public DataGrid<PostDto> list1(int page,int rows) {
		List<PostDto> lists=postService.listPage(page,rows);
		Long count = postDao.count();  
		DataGrid<PostDto> datagrid=new DataGrid<PostDto>();
		datagrid.setRows(lists);
		datagrid.setTotal(count);
		datagrid.setSuccess(true);
		return datagrid;
	}
	/**
     * 3.添加操作
     * @param post
     * @return
     */
	@ResponseBody 
	@RequestMapping("/post/save") 
	public Message save(Post post,int bid,int uid) {
		Message message=new Message();
		Board board=new Board();
		board.setBid(bid);
		User user=new User();
		user.setUid(uid);
		post.setUser(user);
		post.setBoard(board);
		SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		post.setPosttime(sdf.format(new Date()));
		Post post1=postDao.saveAndFlush(post);
		if(post1!=null) {
			message.setMsg("恭喜您，添加成功");
			message.setSuccess(true);
		}else {
			message.setMsg("很遗憾，添加失败");
			message.setSuccess(false);
		}
		return message; 
		}
	/**
     * 4删除操作
     * @param ids
     * @return
     */ 
	@ResponseBody 
    @RequestMapping("/post/deletes") 
	public Message delete(@RequestParam(value = "ids") String ids) {
		Message message=new Message();
		String[] idsStr = ids.split(","); 
		for (int i = 0; i < idsStr.length; i++) { 
			postDao.deleteById(Integer.parseInt(idsStr[i]));
			} 
		message.setMsg("恭喜您，添加成功");
		message.setSuccess(true); 
		return message; 
		}
	/**
	 * 5.更新操作
	 */
	@RequestMapping("/post/update")
	@ResponseBody
	public Message updatePost(Post post,int bid,int pid,int uid) {
		Message message=new Message();
		Board board=new Board();
		board.setBid(bid);
		User user=new User();
		user.setUid(uid);
		post.setUser(user);
		post.setBoard(board);
		SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		post.setPosttime(sdf.format(new Date()));
		Post post1=postDao.saveAndFlush(post);
		//logger.info("user"+user1.getUname()+"更新用户");
		if(post1!=null) {
			message.setMsg("恭喜您，更新成功");
			message.setSuccess(true);
		}else {
			message.setMsg("很遗憾，更新失败");
			message.setSuccess(false);
		}
		return message;
	}
	/**
	 * 6.查询版块列表
	 */
	@ResponseBody 
	@RequestMapping("/board/lists") 
	public List<BoardVo> lists(){
		List<Board> lists=boardDao.findAllBoard();
		List<BoardVo> list1=new ArrayList<>();
		for (Board board : lists) {
			BoardVo boardVo=new BoardVo();
			boardVo.setBid(board.getBid());
			boardVo.setText(board.getBname());
			if(board.getBid()==1) {
				boardVo.setSelected(true);
			}else {
				boardVo.setSelected(false);
			}
			list1.add(boardVo);
		}
		return list1;		
	}
	/**
	 * 6.查询用户列表
	 */
	@ResponseBody 
	@RequestMapping("/user/lists") 
	public List<UserVo> lists1(){
		List<User> lists=userDao.findAll();
		List<UserVo> list1=new ArrayList<>();
		for (User user : lists) {
			UserVo UserVo=new UserVo();
			UserVo.setId(user.getUid());
			UserVo.setText(user.getUname());
			if(user.getUid()==1) {
				UserVo.setSelected(true);
			}else {
				UserVo.setSelected(false);
			}
			list1.add(UserVo);
		}
		return list1;		
	}
}
